package com.niiwoo.civet.account.configuration;

import com.niiwoo.asset.org.business.constants.OrgRabbitConstant;
import com.niiwoo.civet.account.constant.AccountRabbitConstant;
import com.niiwoo.civet.account.constant.MQConstant;
import com.niiwoo.civet.trade.constant.MqConstant;
import com.niiwoo.civet.trade.constant.RabbitConstant;
import com.niiwoo.civet.user.constants.UserRabbitConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfiguration {

    @Bean
    public Exchange ucIdentityExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MQConstant.UC_IDENTITY_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    @Bean
    public Queue ucIdentityDataQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MQConstant.UC_IDENTITY_DATA_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding ucIdentityBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(ucIdentityDataQueue(admin)).to(ucIdentityExchange(admin)).with(MQConstant.UC_IDENTITY_DATA_ROUTING).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Exchange r360OpenAccountExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MQConstant.R360_OPEN_ACCOUNT_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    @Bean
    public Queue r360OpenAccountQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MQConstant.R360_OPEN_ACCOUNT_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding r360OpenAccountBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(r360OpenAccountQueue(admin)).to(r360OpenAccountExchange(admin)).with(MQConstant.R360_OPEN_ACCOUNT_ROUTING).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Exchange withdrawAuditExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MQConstant.WITHDRAW_AUDIT_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    @Bean
    public Queue withdrawAuditDataQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MQConstant.WITHDRAW_AUDIT_DATA_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding withdrawAuditBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(withdrawAuditDataQueue(admin)).to(withdrawAuditExchange(admin)).with(MQConstant.WITHDRAW_AUDIT_DATA_ROUTING).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Exchange fundsFreezeExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MQConstant.FUNDSFREEZE_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    @Bean
    public Queue fundsUnfreezeDataQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MQConstant.FUNDSFREEZE_DATA_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding fundsFreezeBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(withdrawAuditDataQueue(admin)).to(withdrawAuditExchange(admin)).with(MQConstant.FUNDSFREEZE_DATA_ROUTING).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Exchange fundsUnfreezeExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MQConstant.FUNDSUNFREEZE_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    @Bean
    public Queue fundsFreezeDataQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MQConstant.FUNDSUNFREEZE_DATA_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding fundsUnfreezeBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(withdrawAuditDataQueue(admin)).to(withdrawAuditExchange(admin)).with(MQConstant.FUNDSUNFREEZE_DATA_ROUTING).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Exchange projectLendAbnormityExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MqConstant.PROJECT_LEND_ABNORMITY_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    @Bean
    public Queue projectLendAbnormityQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MqConstant.PROJECT_LEND_ABNORMITY_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding doBindingProjectLendAbnormity(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(projectLendAbnormityQueue(admin)).to(projectLendAbnormityExchange(admin))
                .with(MqConstant.PROJECT_LEND_ABNORMITY).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Exchange projectLendContractExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MqConstant.PROJECT_LEND_CONTRACT_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    @Bean
    public Queue projectLendContractQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MqConstant.PROJECT_LEND_CONTRACT_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding doBindingProjectLendContract(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(projectLendContractQueue(admin)).to(projectLendContractExchange(admin))
                .with(MqConstant.PROJECT_LEND_CONTRACT).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Exchange repaymentMessageSendExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(AccountRabbitConstant.Exchange.REPAYMENT_MESSAGE_SEND, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    @Bean
    public Queue repaymentMessageSendQueue(RabbitAdmin admin) {
        Queue queue = new Queue(AccountRabbitConstant.Queue.REPAYMENT_MESSAGE_SEND, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding doBindingRepaymentMessageSend(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(repaymentMessageSendQueue(admin)).to(repaymentMessageSendExchange(admin))
                .with(AccountRabbitConstant.RoutingKey.REPAYMENT_MESSAGE_SEND).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Exchange cardNiuOpenAccountExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MQConstant.CARD_NIU_ACCOUNT_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    @Bean
    public Queue cardNiuOpenAccountQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MQConstant.CARD_NIU_OPEN_ACCOUNT_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding cardNiuOpenAccountBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(cardNiuOpenAccountQueue(admin)).to(cardNiuOpenAccountExchange(admin)).with(MQConstant.CARD_NIU_OPEN_ACCOUNT_ROUTING).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Queue cardNiuRebindBankQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MQConstant.CARD_NIU_REBIND_BANK_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding cardNiuRebindBankBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(cardNiuRebindBankQueue(admin)).to(cardNiuOpenAccountExchange(admin)).with(MQConstant.CARD_NIU_REBIND_CARD_ROUTING).noargs();
        admin.declareBinding(binding);
        return binding;
    }

//    @Bean
//    public Exchange tagAccountFanoutExchange(RabbitAdmin admin) {
//        Exchange exchange = new DirectExchange(BizEventRabbitConstant.Exchange.BIZ_EVENT_ACCOUNT_FANOUT_EXCHANGE, true, true);
//        admin.declareExchange(exchange);
//        return exchange;
//    }
//
//    @Bean
//    public Queue tagAccountFanoutQueue(RabbitAdmin admin) {
//        Queue queue = new Queue(BizEventRabbitConstant.Queue.BIZ_EVENT_ACCOUNT_QUEUE, true);
//        admin.declareQueue(queue);
//        return queue;
//    }
//
//    @Bean
//    public Binding tagAccountFanoutBinding(RabbitAdmin admin) {
//        Binding binding = BindingBuilder.bind(tagAccountFanoutQueue(admin)).to(tagAccountFanoutExchange(admin)).with(BizEventRabbitConstant.Key.BIZ_EVENT_ACCOUNT_KEY).noargs();
//        admin.declareBinding(binding);
//        return binding;
//    }

    /*************还款/垫付发起冻结*****************/
    @Bean
    public Exchange refundFreezeExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MQConstant.REFUND_FREEZE_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }
    @Bean
    public Queue refundFreezeQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MQConstant.REFUND_FREEZE_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }
    @Bean
    public Binding refundFreezeBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(refundFreezeQueue(admin)).to(refundFreezeExchange(admin)).with(MQConstant.REFUND_FREEZE_ROUTING).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    /*************还款/垫付冻结结果*****************/
    @Bean
    public Exchange refundFreezeResultExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MQConstant.REFUND_FREEZE_RESULT_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }
    @Bean
    public Queue refundFreezeResultQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MQConstant.REFUND_FREEZE_RESULT_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding refundFreezeResultBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(refundFreezeResultQueue(admin)).to(refundFreezeResultExchange(admin)).with(MQConstant.REFUND_FREEZE_RESULT_ROUTING).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    /*************还款/垫付解冻结果*****************/
    @Bean
    public Exchange refundUnfreezeExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MQConstant.REFUND_UNFREEZE_RESULT_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }
    @Bean
    public Queue refundUnfreezeQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MQConstant.REFUND_UNFREEZE_RESULT_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }
    @Bean
    public Binding refundUnfreezeBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(refundUnfreezeQueue(admin)).to(refundUnfreezeExchange(admin)).with(MQConstant.REFUND_UNFREEZE_RESULT_ROUTING).noargs();
        admin.declareBinding(binding);
        return binding;
    }


    /**
     * 代扣
     */

    @Bean
    public Exchange withholdApplyExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(MQConstant.WITHHOLD_APPLY_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    @Bean
    public Queue withholdApplyDataQueue(RabbitAdmin admin) {
        Queue queue = new Queue(MQConstant.WITHHOLD_APPLY_DATA_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Binding withholdApplyBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(withholdApplyDataQueue(admin)).to(withholdApplyExchange(admin)).with(MQConstant.WITHHOLD_APPLY_DATA_ROUTING).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Queue orgOpenAccountQueue(RabbitAdmin admin) {
        Queue queue = new Queue(OrgRabbitConstant.Queue.OPEN_ACCOUNT_CALLBACK_QUEUE, true);
        admin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Exchange orgOpenAccountExchange(RabbitAdmin admin) {
        Exchange exchange = new DirectExchange(OrgRabbitConstant.Exchange.ORG_COMMON_DIRECT_EXCHANGE, true, true);
        admin.declareExchange(exchange);
        return exchange;
    }

    @Bean
    public Binding orgOpenAccountBinding(RabbitAdmin admin) {
        Binding binding = BindingBuilder.bind(orgOpenAccountQueue(admin)).to(orgOpenAccountExchange(admin)).with(
                OrgRabbitConstant.getRoutingKey(OrgRabbitConstant.Exchange.ORG_COMMON_DIRECT_EXCHANGE, OrgRabbitConstant.Queue.OPEN_ACCOUNT_CALLBACK_QUEUE)).noargs();
        admin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Exchange NoviceTaskRecordExchange() {
        return new DirectExchange(RabbitConstant.Exchange.NOVICE_TASK_RECORD, true, true);
    }

    @Bean
    public Queue NoviceTaskRecordQueue() {
        return new Queue(RabbitConstant.Queue.NOVICE_TASK_RECORD, true);
    }

    @Bean
    public Binding NoviceTaskRecordBinding() {
        return BindingBuilder.bind(NoviceTaskRecordQueue())
                .to(NoviceTaskRecordExchange())
                .with(RabbitConstant.RoutingKey.NOVICE_TASK_RECORD).noargs();
    }

    @Bean
    public Exchange CallOutTaskAddExchange() {
        return new DirectExchange(UserRabbitConstant.Exchange.CALL_OUT_TASK_ADD, true, true);
    }

    @Bean
    public Queue CallOutTaskAddQueue() {
        return new Queue(UserRabbitConstant.Queue.CALL_OUT_TASK_ADD, true);
    }

    @Bean
    public Binding CallOutTaskAddBinding() {
        return BindingBuilder.bind(CallOutTaskAddQueue())
                .to(CallOutTaskAddExchange())
                .with(UserRabbitConstant.RoutingKey.CALL_OUT_TASK_ADD).noargs();
    }
}
